package com.hnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnu.common.Constants;
import com.hnu.dto.Result;
import com.hnu.entity.Admin;
import com.hnu.service.AdminService;
import com.hnu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @Author hj
 * @Date 2023/12/20 8:49
 * @PackageName:com.hnu.controller
 * @ClassName: AdminController
 * @Description: create a class
 * @Version 1.0
 */
@CrossOrigin //服务器端解决跨域
@RequestMapping("/fileUpload")
@RestController
public class FileUploadController {

    private String FILE_PATH = "D:\\学习\\prcture\\";


    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile file){
        if(file.isEmpty()){
            return Result.error(Constants.UPLOAD_ERROR);
        }
        //获取原始文件名称
        String originalFilename = file.getOriginalFilename();
        //生成随机名称
        String fileName = System.currentTimeMillis()+"."+originalFilename.substring(originalFilename.lastIndexOf(".")+1);
        //设置保存地址
        String path = FILE_PATH;
        File dest = new File(path+fileName);

        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest);
            return Result.ok(Constants.UPLOAD_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(Constants.UPLOAD_ERROR);
        }
    }
}
